<script setup lang="ts">
import { computed, ref, watch } from 'vue';

import { message } from 'ant-design-vue';

interface Props {
  visible: boolean;
  record?: any;
}

interface Emits {
  (e: 'update:visible',value: boolean): void;
  (e: 'success',items: any[]): void
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

// 响应式数据
const selectedCategory = ref('blood');
const selectedItems = ref<string[]>([]);

// 检验项目数据
const labTestItems = ref([
  // 血液检验
  {
    id: 'BT001',
    category: 'blood',
    name: '血常规',
    price: 80,
    description: '红细胞、白细胞、血小板计数',
  },
  {
    id: 'BT002',
    category: 'blood',
    name: '血型鉴定',
    price: 50,
    description: 'ABO血型系统鉴定',
  },
  {
    id: 'BT003',
    category: 'blood',
    name: '凝血功能',
    price: 120,
    description: '凝血酶原时间、活化部分凝血活酶时间',
  },

  // 尿液检验
  {
    id: 'UT001',
    category: 'urine',
    name: '尿常规',
    price: 60,
    description: '尿液物理化学检查',
  },
  {
    id: 'UT002',
    category: 'urine',
    name: '尿沉渣',
    price: 40,
    description: '尿液有形成分检查',
  },

  // 生化检验
  {
    id: 'BC001',
    category: 'biochemistry',
    name: '肝功能',
    price: 150,
    description: 'ALT、AST、ALP、TBIL等',
  },
  {
    id: 'BC002',
    category: 'biochemistry',
    name: '肾功能',
    price: 120,
    description: 'BUN、CREA、UA等',
  },
  {
    id: 'BC003',
    category: 'biochemistry',
    name: '血糖',
    price: 30,
    description: '空腹血糖测定',
  },
  {
    id: 'BC004',
    category: 'biochemistry',
    name: '血脂',
    price: 100,
    description: '胆固醇、甘油三酯等',
  },

  // 微生物检验
  {
    id: 'MC001',
    category: 'microbiology',
    name: '细菌培养',
    price: 200,
    description: '细菌分离培养鉴定',
  },
  {
    id: 'MC002',
    category: 'microbiology',
    name: '药敏试验',
    price: 150,
    description: '抗生素敏感性试验',
  },

  // 影像检查
  {
    id: 'IM001',
    category: 'imaging',
    name: 'X光片',
    price: 120,
    description: '胸部、腹部X线检查',
  },
  {
    id: 'IM002',
    category: 'imaging',
    name: 'B超',
    price: 180,
    description: '腹部超声检查',
  },
  {
    id: 'IM003',
    category: 'imaging',
    name: '心电图',
    price: 100,
    description: '心脏电生理检查',
  },
]);

// 表格列定义
const selectedColumns = [
  {
    title: '项目名称',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '价格',
    dataIndex: 'price',
    key: 'price',
    width: 100,
  },
  {
    title: '操作',
    key: 'action',
    width: 80,
  },
];

// 计算属性
const filteredItems = computed(() => {
  return labTestItems.value.filter(
    (item) => item.category === selectedCategory.value,
  );
});

const selectedItemsData = computed(() => {
  return labTestItems.value.filter((item) =>
    selectedItems.value.includes(item.id),
  );
});

const totalAmount = computed(() => {
  return selectedItemsData.value.reduce((total, item) => total + item.price, 0);
});

// 方法
const onCategoryChange = () => {
  // 切换分类时清空已选项目
  selectedItems.value = [];
};

const toggleItem = (itemId: string) => {
  const index = selectedItems.value.indexOf(itemId);
  if (index === -1) {
    selectedItems.value.push(itemId);
  } else {
    selectedItems.value.splice(index, 1);
  }
};

const removeItem = (itemId: string) => {
  const index = selectedItems.value.indexOf(itemId);
  if (index !== -1) {
    selectedItems.value.splice(index, 1);
  }
};

const handleOk = () => {
  if (selectedItems.value.length === 0) {
    message.warning('请至少选择一个检验项目');
    return;
  }

  // 获取选中的项目数据
  const selectedItemsData = labTestItems.value.filter((item) =>
    selectedItems.value.includes(item.id),
  );

  message.success('检验项目添加成功');
  emit('success', selectedItemsData);
  emit('update:visible', false);

  // 重置选择
  selectedItems.value = [];
};

const handleCancel = () => {
  emit('update:visible', false);
  selectedItems.value = [];
};

// 监听模态框关闭
watch(
  () => props.visible,
  (visible) => {
    if (!visible) {
      selectedItems.value = [];
    }
  },
);
</script>

<template>
  <a-modal
    :open="visible"
    title="添加检验项目"
    width="800px"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <div class="lab-test-modal">
      <!-- 检验分类选择 -->
      <div class="category-section">
        <h4>检验分类</h4>
        <a-radio-group
          v-model:value="selectedCategory"
          @change="onCategoryChange"
        >
          <a-radio-button value="blood">血液检验</a-radio-button>
          <a-radio-button value="urine">尿液检验</a-radio-button>
          <a-radio-button value="biochemistry">生化检验</a-radio-button>
          <a-radio-button value="microbiology">微生物检验</a-radio-button>
          <a-radio-button value="imaging">影像检查</a-radio-button>
        </a-radio-group>
      </div>

      <!-- 检验项目列表 -->
      <div class="items-section">
        <h4>检验项目</h4>
        <div class="items-grid">
          <div
            v-for="item in filteredItems"
            :key="item.id"
            class="item-card"
            :class="{ selected: selectedItems.includes(item.id) }"
            @click="toggleItem(item.id)"
          >
            <div class="item-name">{{ item.name }}</div>
            <div class="item-price">¥{{ item.price.toFixed(2) }}</div>
            <div class="item-desc">{{ item.description }}</div>
          </div>
        </div>
      </div>

      <!-- 已选项目 -->
      <div class="selected-section" v-if="selectedItems.length > 0">
        <h4>已选项目</h4>
        <a-table
          :columns="selectedColumns"
          :data-source="selectedItemsData"
          :pagination="false"
          size="small"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'price'">
              ¥{{ record.price.toFixed(2) }}
            </template>
            <template v-else-if="column.key === 'action'">
              <a-button
                type="text"
                danger
                size="small"
                @click="removeItem(record.id)"
              >
                删除
              </a-button>
            </template>
          </template>
        </a-table>

        <div class="total-section">
          <span class="total-label">总计:</span>
          <span class="total-amount">¥{{ totalAmount.toFixed(2) }}</span>
        </div>
      </div>
    </div>
  </a-modal>
</template>

<style scoped>
.lab-test-modal {
  max-height: 600px;
  overflow-y: auto;
}

.category-section {
  margin-bottom: 24px;
}

.category-section h4 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  color: #262626;
}

.items-section {
  margin-bottom: 24px;
}

.items-section h4 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  color: #262626;
}

.items-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
}

.item-card {
  padding: 12px;
  cursor: pointer;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  transition: all 0.3s;
}

.item-card:hover {
  border-color: #1890ff;
}

.item-card.selected {
  background: #e6f7ff;
  border-color: #1890ff;
}

.item-name {
  margin-bottom: 4px;
  font-weight: 600;
  color: #262626;
}

.item-price {
  margin-bottom: 4px;
  font-size: 14px;
  font-weight: 600;
  color: #f5222d;
}

.item-desc {
  font-size: 12px;
  line-height: 1.4;
  color: #666;
}

.selected-section {
  padding-top: 16px;
  border-top: 1px solid #f0f0f0;
}

.selected-section h4 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  color: #262626;
}

.total-section {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
  padding-top: 12px;
  margin-top: 16px;
  border-top: 1px solid #f0f0f0;
}

.total-label {
  font-size: 14px;
  color: #666;
}

.total-amount {
  font-size: 18px;
  font-weight: 700;
  color: #f5222d;
}
</style>
